home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / d_hpgl.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  13KB  |  493 lines

  1. /*--------------------------------------------------------------*/
  2. /*      HPGL Driver 1.0 , for GLE V3.0          */
  3. /*---------------------------------------------------------------------------*/
  4. #include "all.h"
  5. #include <math.h>
  6. #include "core.h"
  7. #include "mygraph.h"
  8. #include "mydev.h"
  9. #define GS 29
  10. #define FF 12
  11. int32 hp_clipx, hp_clipy;
  12. static int32 hpgl_addx, hpgl_addy;
  13. extern int hpgl_size; /* a0, a1, a2, a3, a4 */
  14. int ingraphmode;
  15. extern struct gmodel g;
  16. /*---------------------------------------------------------------------------*/
  17. #define pi 3.141592653
  18. #define true (!false)
  19. #define BLACKANDWHITE 1
  20. #define ESC 27
  21. #define false 0
  22. #define dbg if ((gle_debug & 64)>0)
  23. #define stop if ((gle_debug & 128)>0) return
  24. extern int gle_debug;
  25. int incap=true;
  26. int getch(void);
  27. /*---------------------------------------------------------------------------*/
  28. /* The global variables for the PC screen driver */
  29. /*-----------------------------------------------*/
  30.  
  31. int i,l,j,ix,iy;
  32. double f;
  33. double devxcm,devycm;  /* settable via environment variables (or symbols*/
  34. FILE *hpfile;
  35.  
  36. #define gerr() i = graphresult(); if (i!=0) printf("Graph error: %s \n",grapherrormsg(i));
  37. #define sx(v) ( ((v) * d_xscale) + hpgl_addx)
  38. #define sy(v) ( (((v) * d_yscale)) + hpgl_addy)
  39. #define rx(v) ( ((v) * d_xscale))
  40. #define ry(v) ( (((v) * d_yscale)))
  41.  
  42. double d_scale, d_xscale, d_yscale;
  43. int d_graphmode;
  44. int d_fillstyle=1,d_fillcolor;
  45. int d_lstyle,d_lwidth;
  46. char outstr[400];
  47.  
  48. d_devcmd(char *s)
  49. {}
  50. /*---------------------------------------------------------------------------*/
  51. d_dfont(char *c)
  52. {
  53.     /* only used for the DFONT driver which builds fonts */
  54. }
  55. /*---------------------------------------------------------------------------*/
  56. static char lastline[80];
  57. d_message(char *s)
  58. {
  59.     static int single_step;
  60.     int oldcolor,oldx,oldy;
  61.     printf("%s\n",s);
  62. }
  63. /*---------------------------------------------------------------------------*/
  64. d_source(char *s)
  65. {
  66.     s=s;
  67. }
  68. /*---------------------------------------------------------------------------*/
  69. d_get_type(char *t)
  70. {
  71.     strcpy(t,"HARDCOPY, HPGL, VAX, PC");
  72. }
  73. /*---------------------------------------------------------------------------*/
  74. d_set_path(int onoff)
  75. {}
  76. /*---------------------------------------------------------------------------*/
  77. d_newpath()
  78. {
  79. }
  80. /*---------------------------------------------------------------------------*/
  81. extern char output_file[];
  82. extern char input_file[];
  83. d_open(double width, double height)
  84. {
  85.     char *outfile;
  86.     char *s;
  87.     char outfileb[90] = "out.hp";
  88.  
  89.     outfile = &outfileb[0];
  90.     s = strrchr(input_file,'.');
  91.     if (s!=NULL) {
  92.         strcpy(outfile,input_file);
  93.         s = strrchr(outfile,'.');
  94.         strcpy(s,".hp");
  95.     }
  96.     if (output_file[0]!=0) outfile = &output_file[0];
  97. #ifdef __TURBOC__
  98.     printf("\n Writing output to file (%s) \n",outfile);
  99. #endif
  100.     hpfile = fopen(outfile,"w");
  101.     if (hpfile == NULL) {
  102.         gprint("Unable to HP output file {%s} \n",outfile);
  103.         gle_abort("Unable to open HP output file \n");
  104.     }
  105.  
  106.     hpgl_initstr(); /* read environment variables for set sequences */
  107.     graphmode();
  108.     ingraphmode = true;
  109.  
  110.     /* Get largest rectangle we can fit on the screen */
  111.     d_scale = devxcm / width;
  112.     f = devycm / height;
  113.     if (f<d_scale) d_scale = f;
  114.     if (d_scale<1) gprint("Shrinking picture to fit by factor %g \n",d_scale);
  115.     if (d_scale>1) d_scale = 1; /* scale down not up */
  116.  
  117.     d_xscale = d_scale * 400; /*  MAXX / devxcm;  Device Scale X, Device Scale y */
  118.     d_yscale = d_scale * 400; /*  MAXY / devycm; */
  119.  
  120.     hp_clipx = 400L * (devxcm) + 1;
  121.     hp_clipy = 400L * (devxcm) + 1;
  122.  
  123.     g.userwidth = width;
  124.     g.userheight = height;
  125.     plotter_fonts();
  126. }
  127. /*---------------------------------------------------------------------------*/
  128. d_tidyup()
  129. {
  130. #ifdef DJ       /* a.r. */
  131.     djtextmode();
  132. #else
  133.     textmode();
  134. #endif
  135. }
  136. d_close()
  137. {
  138.     g_flush();
  139. #ifdef DJ       /* a.r. */
  140.     djtextmode();
  141. #else
  142.     textmode();
  143. #endif
  144. }
  145. /*---------------------------------------------------------------------------*/
  146. d_set_line_cap(int i)
  147. {
  148.     i++;
  149. }
  150. /*---------------------------------------------------------------------------*/
  151. d_set_line_join(int i)
  152. {
  153.     i++;
  154. }
  155. /*---------------------------------------------------------------------------*/
  156. d_set_line_miterlimit(double d)
  157. {
  158.     i++;
  159. }
  160. /*---------------------------------------------------------------------------*/
  161. d_set_line_width(double w)
  162. {
  163. }
  164. /*---------------------------------------------------------------------------*/
  165. d_set_line_styled(double dd)
  166. {}
  167. double lstylewidths[10]; /* widths */
  168. int nlstyle=0;  /* number in array, if zero then solid line */
  169. int lstylei=0;  /* entry we are drawing */
  170. double lstylex=0;       /* distance along current entry */
  171. int lstyleblack=true;
  172. d_line_dash(double zx, double zy)
  173. {
  174.     double ix,iy,ux,uy,dx,dy,d,dd,ddx,ddy,nx,ny,totald;
  175.     int newblack;
  176.     dx = zx - g.curx; dy = zy - g.cury;
  177.     d = sqrt(dx*dx + dy*dy);
  178.     totald = d;
  179.     if (totald==0) return;
  180.     for (;d>0;) {
  181.         if (d>(lstylewidths[lstylei]-lstylex)) {
  182.             dd = lstylewidths[lstylei++]-lstylex;
  183.             newblack = !lstyleblack;
  184.             lstylex = 0;
  185.             if (lstylei>=nlstyle) { lstylei = 0; newblack =true;}
  186.         } else {
  187.             dd = d;
  188.             newblack = lstyleblack;
  189.             lstylex += dd;
  190.         }
  191.         d -= dd;
  192.         ddx = dx *   dd/totald;  ddy = dy*dd/totald;
  193.         nx = g.curx + ddx; ny = g.cury + ddy;
  194.         if (lstyleblack) dd_line(nx,ny); else g.xinline = false;
  195.         g.curx = nx; g.cury = ny;
  196.         lstyleblack = newblack;
  197.     }
  198. }
  199. d_set_line_style(char *s)
  200. {
  201.     /* should deal with [] for solid lines */
  202.     static char *defline[] = {"","","12","42","14","92","1282"
  203.     ,"9229","4114","54","73","7337","6261","2514"};
  204.     static char ob[200];
  205.     int l;
  206.  
  207.     if (!g.inpath) g_flush();
  208.     strcpy(ob,"[");
  209.     if (strlen(s)==1) s = defline[*s-'0'];
  210.     l = strlen(s);
  211.     for (i=0;i<l;i++)
  212.         lstylewidths[i] = (*(s+i)-'0')*g.lstyled;
  213.     nlstyle = l;
  214. }
  215. /*---------------------------------------------------------------------------*/
  216. d_fill()
  217. {
  218. }
  219. /*---------------------------------------------------------------------------*/
  220. d_fill_ary(int nwk,double (*wkx)[],double (*wky)[])
  221. {
  222.     int i;
  223. /*      fprintf(psfile,"%g %g moveto \n",(*wkx)[0],(*wky)[0]);
  224.     for (i=1;i<nwk;i++)
  225.         fprintf(psfile,"%g %g l \n",(*wkx)[i],(*wky)[i]);
  226. */
  227. }
  228. d_line_ary(int nwk,double (*wkx)[],double (*wky)[])
  229. {
  230.     int i;
  231. /*
  232.     g_dev( (*wkx)[0], (*wky)[0], &ix, &iy);
  233.     moveto(ix,iy);
  234.     for (i=1;i<nwk;i++) {
  235.         g_dev( (*wkx)[i], (*wky)[i], &ix, &iy);
  236.         lineto(ix,iy);
  237.     }
  238. */
  239. }
  240. /*---------------------------------------------------------------------------*/
  241. d_stroke()
  242. {
  243. }
  244. /*---------------------------------------------------------------------------*/
  245. d_clip()
  246. {
  247. }
  248. /*---------------------------------------------------------------------------*/
  249. d_set_matrix(double newmat[3][3])
  250. {
  251. }
  252. /*---------------------------------------------------------------------------*/
  253. d_move(double zx,double zy)
  254. {
  255. }
  256. /*---------------------------------------------------------------------------*/
  257. d_reverse()     /* reverse the order of stuff in the current path */
  258. {
  259. }
  260. /*---------------------------------------------------------------------------*/
  261. d_closepath()
  262. {
  263.     g_line(g.closex,g.closey);
  264. }
  265. /*---------------------------------------------------------------------------*/
  266. int hpglxy(double xx, double yy);
  267. d_line(double zx,double zy)
  268. {
  269.     if (nlstyle==0) dd_line(zx,zy);
  270.     else d_line_dash(zx,zy);
  271. }
  272. dd_line(double zx,double zy)
  273. {
  274.     static double ix,iy,ux,uy;
  275.     static double oldx= -1000,oldy= -1000;
  276.     char s1[40],s2[40];
  277.     if (g.xinline==false) {
  278.         g_dev(g.curx,g.cury,&ux,&uy);
  279.         if (oldx!=ux || oldy!=uy) {
  280.          strcat(outstr,"; PU ; PA ");
  281.          hpglxy(ux,uy);
  282.         }
  283.         strcat(outstr,"; PD ; PA ");
  284.         g.xinline = true;
  285.     }
  286.     g_dev(zx,zy,&ix,&iy);
  287.     hpglxy(ix,iy);
  288.     oldx = ix; oldy = iy;
  289.     if (strlen(outstr)>70) d_flush();
  290. }
  291. /*---------------------------------------------------------------------------*/
  292. hpglxy(double xx, double yy)
  293. {
  294.     int32 x,y;
  295.     char ss[30];
  296.     x = sx(xx); y = sy(yy);
  297. /*      if (x<0) x = 0;
  298.     if (x>hp_clipx) x = hp_clipx;
  299.     if (y<0) y = 0;
  300.     if (y>hp_clipy) y = hp_clipy;
  301. */
  302.     sprintf(ss,"%ld %ld ",x,y);
  303.     strcat(outstr,ss);
  304. }
  305. /*---------------------------------------------------------------------------*/
  306. d_clear()
  307. {
  308.  }
  309. /*---------------------------------------------------------------------------*/
  310. d_flush()
  311. {
  312.     fprintf(hpfile,"%s;\n PA ",outstr);
  313.     outstr[0] = 0;
  314. }
  315. /*---------------------------------------------------------------------------*/
  316. d_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  317. {
  318.     df_arcto(x1,y1,x2,y2,rrr);
  319. }
  320. /*---------------------------------------------------------------------------*/
  321. d_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  322. {
  323.     df_arc(r,t1,t2,cx,cy);
  324. }
  325. /*---------------------------------------------------------------------------*/
  326. d_narc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  327. {
  328.     /* swap t1 t2 */
  329.     df_arc(r,t2,t1,cx,cy);
  330. }
  331. /*---------------------------------------------------------------------------*/
  332. d_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  333. {
  334.     df_box_fill(x1,y1,x2,y2);
  335. }
  336. d_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  337. {
  338.     df_box_stroke(x1,y1,x2,y2);
  339. }
  340. /*---------------------------------------------------------------------------*/
  341. d_circle_stroke(double zr)
  342. {
  343.     df_circle_stroke(zr);
  344. }
  345. d_circle_fill(double zr)
  346. {
  347.     df_circle_fill(zr);
  348. }
  349. /*---------------------------------------------------------------------------*/
  350. d_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  351. {
  352.     double ax,bx,cx,ay,by,cy,dist;
  353.     double xxx,yyy,i,t,nstep,x0,y0;
  354.     g_get_xy(&x0,&y0);
  355.     dist = fabs(x3-x0) + fabs(y3-y0);
  356.     nstep = 10;
  357.     if (dist>3) nstep = 20;
  358.     if (dist<.5) nstep = 5;
  359.     if (dist<.3) nstep = 3;
  360.     if (dist<.1) {
  361.         g_line(x3,y3);
  362.         return;
  363.     }
  364.     cx = (x1-x0)*3;
  365.     bx = (x2-x1)*3-cx;
  366.     ax = x3-x0-cx-bx;
  367.     cy = (y1-y0)*3;
  368.     by = (y2-y1)*3-cy;
  369.     ay = y3-y0-cy-by;
  370.     for (i=0;i<=nstep;i++) {
  371.         t = i/nstep;
  372.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  373.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  374.         g_line(xxx,yyy);
  375.     }
  376. }
  377. /*---------------------------------------------------------------------------*/
  378. d_set_color(int32 f)
  379. {
  380.     int i;
  381.     char ss[50];
  382.     colortyp  cc;
  383.     cc.l = f;
  384.     i = 1;
  385.     if (cc.b[B_R]>100) i = 2;
  386.     if (cc.b[B_G]>100) i = 3;
  387.     if (cc.b[B_B]>100) i = 4;
  388.     if (cc.b[B_R]>100 && cc.b[B_G]>100) i = 5;
  389.     if (cc.b[B_B]>100 && cc.b[B_G]>100) i = 5;
  390.     if (cc.b[B_R]>250 && cc.b[B_G]>250 && cc.b[B_B]>250) i = 6;
  391.     sprintf(ss,"; SP%d ; PA ",i);
  392.     strcat(outstr,ss);
  393. }
  394. d_set_fill(int32 f)
  395. {
  396. }
  397. /*---------------------------------------------------------------------------*/
  398. d_beginclip()
  399. {
  400. }
  401. d_endclip()
  402. {
  403. }
  404. struct char_data {float wx,wy,x1,y1,x2,y2; };
  405. int font_get_chardata(struct char_data **cd, int ff, int cc);
  406. /*---------------------------------------------------------------------------*/
  407. d_char(int font, int cc)
  408. {
  409.     static struct char_data cd;
  410.     static int ix,iy,ix1,ix2,iy1,iy2,fz,fzx;
  411.     static int ux,uy;
  412.     static int safnt;
  413.     char ss[2];
  414.  
  415.     ss[0] = cc;
  416.     ss[1] = 0;
  417.     if (safnt==0) safnt = pass_font("PLSR");
  418.     if (font>=2) {
  419.         my_char(font,cc);
  420.         return;
  421.     }
  422.     my_char(safnt,cc);
  423. }
  424. char *hpglopenstr,*hpglclosestr;
  425. int inithpgl;
  426. graphmode()
  427. {
  428.     if (!inithpgl) hpgl_initstr();
  429.     ingraphmode = true;
  430.     fprintf(hpfile,"%s\n",hpglopenstr);
  431. }
  432. #ifdef DJ       /* a.r. */
  433. djtextmode()
  434. #else
  435. textmode()
  436. #endif
  437. {
  438.     if (!inithpgl) hpgl_initstr();
  439.     fprintf(hpfile,";\n %s \n",hpglclosestr);
  440.     ingraphmode = false;
  441. }
  442. char *cvt_control(char *mystr);
  443. char *cvt_control(char *xx)
  444. {
  445.     char *s;
  446.     static char mystr[200];
  447.     strcpy(mystr,xx);
  448. loop1:  s = strchr(mystr,'^');
  449.     if (s!=NULL) {
  450.         *(s+1) = *(s+1) - 64;
  451.         memmove(s,s+1,strlen(s+1)+1);
  452.         goto loop1;
  453.     }
  454.     return mystr;
  455. }
  456. double getsymvalue(char *sym, double dflt);
  457. char *getsymbol(char *sym);
  458. double getsymvalue(char *sym, double dflt)
  459. {
  460.     char *s;
  461.     s = getsymbol(sym);
  462.     if (strlen(s)==0) return dflt;
  463.     return atof(s);
  464. }
  465. hpgl_initstr()
  466. {
  467.     char  *s;
  468.     double def_x,def_y;
  469.     inithpgl = true;
  470.     s = getsymbol("HPGL_OPEN");
  471.     /* Init plotter and scale so that 400 units = 1cm */
  472.     /* s = "^[.(^[.I;;17:^[.N;19:IN;IP 520,1140,15720,11140; sc 120,15320,540,10540;PU;VS10;SP1;PA;PU;"; */
  473.  
  474.  
  475.     if (strlen(s)==0) s = "^[.(^[.I;;17:^[.N;19:IN;IP ;PU;VS10;SP1;PA;PU;";
  476.     hpglopenstr = sdup(cvt_control(s));
  477.     devxcm = getsymvalue("HPGL_WIDTH",200);
  478.     devycm = getsymvalue("HPGL_HEIGHT",200);
  479.     def_x = .9; def_y = 1.5;
  480.     if (hpgl_size==0) { def_x = -22190.0/400; def_y = -15740.0/400; }
  481.     if (hpgl_size==1) { def_x = -15120.0/400; def_y = -10760.0/400; }
  482.     hpgl_addx = 400*getsymvalue("HPGL_ADDX",def_x);
  483.     hpgl_addy = 400*getsymvalue("HPGL_ADDY",def_y);
  484.  
  485.     /* printf("width %g hei %g str {%s} \n",devxcm,devycm,s); */
  486.     s = getsymbol("HPGL_CLOSE");
  487.     /* Advance Full and cut page */
  488.     if (strlen(s)==0) s = "SP0;IN;AF;^[.)";
  489. /*      if (strlen(s)==0) s = "SP0;IN;AH;^[.)"; */
  490.     hpglclosestr = sdup(cvt_control(s));
  491. }
  492.  
  493.